package com.haizhi.crm.tag.manager.impl;

import com.alibaba.fastjson.JSON;
import com.haizhi.crm.common.constant.TagStatus;
import com.haizhi.crm.common.exception.UnexpectedStatusException;
import com.haizhi.crm.common.rest.RestService;
import com.haizhi.crm.tag.manager.TagCategoryManager;
import com.haizhi.crm.tag.model.TagCategoryReq;
import com.haizhi.crm.tag.model.TagCategoryResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by chengmo on 2018/5/18.
 */
@Service
public class TagCategoryManagerImpl implements TagCategoryManager {

    private static final String PATH_CATEGORY = "/category";
    private static final String PATH_FIND_ALL = PATH_CATEGORY + "/findAll";
    private static final String PATH_SAVE_OR_UPDATE = PATH_CATEGORY + "/saveOrUpdate";
    private static final String PATH_DELETE = PATH_CATEGORY + "/delete";

    @Value("${tag.manager.url.graph-tag-analytics:}" )
    private String restUrl;
    @Autowired
    private RestService restService;

    @Override
    public TagCategoryResp findAll(TagCategoryReq request) {
        return this.doPost(PATH_FIND_ALL, request);
    }

    @Override
    public TagCategoryResp saveOrUpdate(TagCategoryReq request) {
        return this.doPost(PATH_SAVE_OR_UPDATE, request);
    }

    @Override
    public TagCategoryResp delete(TagCategoryReq request) {
        return this.doPost(PATH_DELETE, request);
    }

    ///////////////////////
    // private functions
    ///////////////////////
    private TagCategoryResp doPost(String path, TagCategoryReq request) {
        try {
            return restService.doPost(restUrl + path, request, TagCategoryResp.class);
        } catch (Exception e) {
            throw new UnexpectedStatusException(TagStatus.CALL_REST_ERROR, e,
                    JSON.toJSONString(request, true));
        }
    }
}
